#ifndef RMVIDEOCAPTURE_H
#define RMVIDEOCAPTURE_H
#include <opencv2/opencv.hpp>
using namespace cv;


class RMVideoCapture
{
public:
    RMVideoCapture(const char* device, unsigned int size_buffer = 1);
    ~RMVideoCapture();
    bool startStream();
    bool closeStream();
    bool setExposureTime(bool auto_exp, int t);
    bool setVideoFormat(int width, int height, bool mjpg = 1);
    bool changeVideoFormat(int width, int height, bool mjpg = 1);
    bool getVideoSize(int &width, int &height);
    
    bool setVideoFPS(int fps);
    bool setBufferSize(int bsize);
    void restartCapture();
    int getFrameCount(){
        return current_frame;
    }
    
    void info();
    
    RMVideoCapture& operator >> (Mat & image);
    
private:
    void cvtRaw2Mat(const void* data, Mat &image);
    bool refreshVideoFormat();
    bool initMMap();
    int xioctl(int fd, int request, void *arg);
    
    struct MapBuffer
    {
        void *ptr;
        unsigned int size;
    };
    unsigned int capture_width;
    unsigned int capture_height;
    unsigned int format;
    int fd;
    unsigned int buffer_size;
    unsigned int buffer_index;
    unsigned int current_frame;
    MapBuffer *mb;
    const char* video_path;
};

#endif // RMVIDEOCAPTURE_H
